package com.jzo2o.health.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.trade.NativePayApi;
import com.jzo2o.api.trade.dto.request.NativePayReqDTO;
import com.jzo2o.api.trade.dto.response.NativePayResDTO;
import com.jzo2o.api.trade.enums.PayChannelEnum;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.health.constant.TradeConstants;
import com.jzo2o.health.enums.OrderPayStatusEnum;
import com.jzo2o.health.mapper.OrdersMapper;
import com.jzo2o.health.model.domain.Orders;
import com.jzo2o.health.model.dto.response.OrdersPayResDTO;
import com.jzo2o.health.properties.TradeProperties;
import com.jzo2o.health.service.IOrdersMangerService;
import com.jzo2o.health.service.IOrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Description OrdersMangerServiceImpl
 * @Author kkk
 * 订单表 用于支付、退款
 * @Date 2024-12-14
 */
@Service
@Slf4j
public class OrdersMangerServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersMangerService {

    @Autowired
    private NativePayApi nativePayApi;

    @Autowired
    private TradeProperties tradeProperties;

    @Autowired
    private IOrdersService ordersService;

    //订单支付
    @Override
    public OrdersPayResDTO pay(Long id, PayChannelEnum tradingChannel) {

        //判断订单是否存在
        Orders orders = this.getById(id);
        if (ObjectUtil.isNull(orders)) {
            throw new BadRequestException("订单不存在");
        }

        //判断订单是否处理过(已支付且有交易订单号)
        if (ObjectUtil.equal(orders.getPayStatus(), OrderPayStatusEnum.PAY_SUCCESS.getStatus())
                && ObjectUtil.isNotNull(orders.getTradingOrderNo())) {
            OrdersPayResDTO ordersPayResDTO = BeanUtils.copyBean(orders, OrdersPayResDTO.class);
            ordersPayResDTO.setProductOrderNo(id);
            return ordersPayResDTO;
        }

        //调用feign的扫码支付接口获取支付二维码给前端
        NativePayReqDTO nativePayDTO = new NativePayReqDTO();

        //封装接口请求参数
        nativePayDTO.setProductAppId(TradeConstants.PRODUCT_APP_ID);//业务系统标识
        nativePayDTO.setProductOrderNo(orders.getId());//业务系统订单号

        PayChannelEnum payChannelEnum = PayChannelEnum.WECHAT_PAY;//默认支付渠道为微信支付
        Long enterpriseId = tradeProperties.getWechatEnterpriseId();//默认账户号为微信支付商户id
        if (ObjectUtils.notEqual(payChannelEnum, tradingChannel)) {
            payChannelEnum = PayChannelEnum.ALI_PAY;
            enterpriseId = tradeProperties.getAliEnterpriseId();
        }

        nativePayDTO.setEnterpriseId(enterpriseId);//商户号
        nativePayDTO.setTradingChannel(payChannelEnum);//支付渠道
        nativePayDTO.setTradingAmount(BigDecimal.valueOf(0.01));
        nativePayDTO.setMemo(orders.getSetmealName());//备注

        Boolean changeChannel= false;//默认未更换过支付渠道
        if (ObjectUtils.notEqual(orders.getTradingChannel(),tradingChannel.getValue())){
            changeChannel = true;//说明更换过支付渠道
        }

        nativePayDTO.setChangeChannel(changeChannel);//是否切换支付渠道

        //调用接口
        NativePayResDTO nativePayResDTO = nativePayApi.createDownLineTrading(nativePayDTO);

        //判断接口调用是否成功
        if (ObjectUtils.isNull(nativePayResDTO)){
            throw new BadRequestException("调用扫码支付接口失败");
        }

        //更新订单表数据
        ordersService.updateOrder(id,nativePayResDTO);

        //封装数据返回
        OrdersPayResDTO ordersPayResDTO = BeanUtils.copyBean(nativePayResDTO, OrdersPayResDTO.class);
        ordersPayResDTO.setPayStatus(orders.getPayStatus());
        return ordersPayResDTO;
    }
}
